﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DataAccess;
using System.Data.OleDb;
using System.Drawing;


public partial class FinanceUsers_InsertProductsRepository : System.Web.UI.Page
{
   static int ReposDeviceId;
    protected void Page_Load(object sender, EventArgs e)
    {
        txtTime.Enabled = false;
        txtTime.Text = DateTime.Now.ToString();
        lblMessage.Text = "";
        lblMsgFile.Text = "";
      

    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (Session["Edit"] != null)
            {
                if (Session["Edit"].ToString() == "True")
                {
                    TblRepository.Destroy(ReposDeviceId);
                    Session["Edit"] = "false";

                }
            }
            string StrFile = System.IO.Path.GetExtension(FileUploadExcel.FileName.ToLower());
            if (StrFile == ".xlsx")
            {
                string fileName = FileUploadExcel.ResolveClientUrl(FileUploadExcel.PostedFile.FileName);
                DataTable dtExcel = new DataTable();

                string SourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fileName + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'";
                OleDbConnection con = new OleDbConnection(SourceConstr);
                string query = "Select * from [Sheet1$]";
                OleDbDataAdapter data = new OleDbDataAdapter(query, con);
                data.Fill(dtExcel);
                string s = dtExcel.Rows[0]["Name"].ToString();
                int count = 0;
                foreach (var item in dtExcel.Rows)
                {
                    count++;

                }

                if (count == Convert.ToInt32(txtCount.Text))
                {
                    TblRepository Repos = new TblRepository();
                    Repos.DeviceId = Convert.ToInt32(drDevice.SelectedValue);
                    Repos.DateX = Convert.ToDateTime(txtTime.Text);
                    Repos.Count = Convert.ToInt32(txtCount.Text);
                    Repos.Save();



                    for (int i = 0; dtExcel.Rows.Count > i; i++)
                    {
                        TblReposDetail ReposD = new TblReposDetail();
                        ReposD.IdRepos = Repos.Id;
                        ReposD.Serial = dtExcel.Rows[i]["Name"].ToString();
                        ReposD.Available = true;
                        ReposD.Save();
                    }

                    lblMessage.ForeColor = Color.Green;
                    lblMessage.Text = "ثبت اطلاعات با موفقیت صورت گرفت";
                    GrwDeviceRpos.DataBind();
                    txtCount.Text = "";

                }
                else
                {
                    lblMessage.ForeColor = Color.Red;
                    lblMessage.Text = "تعداد سریال ها با فایل ورودی مطابقت ندارد";
                }

            }
            else
            {

                lblMsgFile.Text = "فایل اکسل را انتخاب کنید";
            }
        }
        

    }

    protected void GrwDeviceRpos_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      
        




            if (e.CommandName == "Edit")
            {
                ReposDeviceId = Convert.ToInt32(e.CommandArgument.ToString());
                TblRepository repo = TblRepository.FetchByID(ReposDeviceId);
                txtCount.Text = repo.Count.ToString();
                drDevice.SelectedValue = repo.DeviceId.ToString();
                Session["Edit"] = "True";

            }
      
    }


    protected void btnCancle_Click(object sender, EventArgs e)
    {
        txtCount.Text = "";
        
    }
}
